import { apiProxy } from "@/utils/request"
export default {
    state: {
        cartGoods: []
    },
    getters: {
        getCartGoods(state) {
            return state.cartGoods
        }
    },
    mutations: {
        addCart(state, g) {
            state.cartGoods.push(g)
        },
        getCart(state, goods) {
            state.cartGoods = goods
        }
    },
    actions: {
        addCartAsync({ commit }, data) {
            console.log(data, 'data');
            return new Promise((resolve) => {
                console.log(commit, 'commit');
                apiProxy({
                    url: "/api/cart/",
                    method: "post",
                    data
                }).then(res => {
                    //加入成功
                    if (res.code == 0) {
                      
                        //再次请求购物车获取最新购物车数据
                        this.dispatch("getCaryAsync").then(res => {
                            console.log('更行', res);
                        })
                    }
                    resolve(res)
                })

            })
        },

        putCaryAsync({ commit }, data) {
            console.log(commit, 'commit');
            console.log(data, "data123");
            return new Promise((resolve) => {

                apiProxy({
                    url: "/api/cart",
                    method: "put",
                    data: {
                        id: data.data.id,
                        //id:'3945',//商品id
                        num: data.data.num,//商品数量
                        multi_buy_limit: data.data.multi_buy_limit,//最大数量
                    }

                }).then(res => {
                    //删除成功
                    console.log(res, "返回res");
                    if (res.code == 0) {
                        //删除成功再次更行
                        this.dispatch("getCaryAsync").then(res => {
                            console.log('更行', res);
                        })
                    } else {
                        console.log('更行失败');
                    }
                    resolve('修改成功')
                })
            })

        },
        getCaryAsync({ commit }) {
            return new Promise((resolve) => {
                apiProxy({
                    url: '/api/cart',
                    method: 'get'
                }).then(res => {
                    if (res.code == 0) {
                        commit('getCart', res.data)
                    }
                    resolve(res)
                })
            })

        },
        updateCartAsync({ commit }, good) {
            console.log(commit, 'commit');
            return new Promise(resolve => {
                apiProxy({
                    url: `api/cart`,
                    method: "put",
                    data: good
                }).then(res => {
                    console.log(res, '选择成功123');
                    if (res.code == 0) {
                        this.dispatch("getCaryAsync").then(res => {
                            console.log('更行', res);
                        })
                    }
                    resolve("选择成功")
                })
            })
        },
        deleteCartAsync({commit},id){
            console.log(commit,'commit');
            return new Promise(resolve => {
                apiProxy({
                    url: `api/cart/${id}`,
                    method: "delete",
                   
                }).then(res => {
                   
                    if (res.code == 0) {
                        this.dispatch("getCaryAsync").then(res => {
                            console.log('更行', res);
                        })
                    }
                    resolve("删除成功")
                })
            })
        }
    },
    modules: {
    }
}